package com.ruoyi.web.controller.yf;

import java.util.List;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.yf.domain.YfMedicalWasteTreatmentManager;
import com.ruoyi.yf.domain.YfYcgx;
import com.ruoyi.yf.service.IYfMedicalWasteTreatmentManagerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yf.domain.YfMedicalWasteAndHospitalRelationship;
import com.ruoyi.yf.service.IYfMedicalWasteAndHospitalRelationshipService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 医废处置机构、医疗机构关系Controller
 *
 * @author XiaoYi...
 * @date 2020-08-14
 * 4585ba65-4387-4fd1-9ebd-44f76d114f16
 */
@Controller
@RequestMapping("/yf/medicalHospitalRelationship")
public class YfMedicalWasteAndHospitalRelationshipController extends BaseController {
    private String prefix = "yf/medicalHospitalRelationship";

    @Autowired
    private IYfMedicalWasteAndHospitalRelationshipService yfMedicalWasteAndHospitalRelationshipService;


    //机构机构医院
    @Autowired
    private ISysDeptService deptService;

    @RequiresPermissions("yf:medicalHospitalRelationship:view")
    @GetMapping()
    public String medicalHospitalRelationship() {
        return prefix + "/medicalHospitalRelationship";
    }

    /**
     * 查询医废处置机构、医疗机构关系列表
     * 30f9c88e-c878-4aa0-bbca-95a61ca6e179
     */
    @RequiresPermissions("yf:medicalHospitalRelationship:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YfMedicalWasteAndHospitalRelationship yfMedicalWasteAndHospitalRelationship) {
        startPage();
        List<YfMedicalWasteAndHospitalRelationship> list = yfMedicalWasteAndHospitalRelationshipService.selectYfMedicalWasteAndHospitalRelationshipList(yfMedicalWasteAndHospitalRelationship);
        return getDataTable(list);
    }


    /**
     * 验证机构是否签订合同
     * c5cba67b-ef36-410d-84a9-937ceefe2dc3
     */
    @PostMapping("/checkMedicalWasteContract")
    @ResponseBody
    public String checkMedicalWasteContract(Long deptId) {
        return yfMedicalWasteAndHospitalRelationshipService.checkMedicalWasteContract(deptId);
    }


    /**
     * 导出医废处置机构、医疗机构关系列表
     * 2bdb1f3e-2c7c-4011-893d-b872820ef038
     */
    @RequiresPermissions("yf:medicalHospitalRelationship:export")
    @Log(title = "医废处置机构、医疗机构关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YfMedicalWasteAndHospitalRelationship yfMedicalWasteAndHospitalRelationship) {
        List<YfMedicalWasteAndHospitalRelationship> list = yfMedicalWasteAndHospitalRelationshipService.selectYfMedicalWasteAndHospitalRelationshipList(yfMedicalWasteAndHospitalRelationship);
        ExcelUtil<YfMedicalWasteAndHospitalRelationship> util = new ExcelUtil<YfMedicalWasteAndHospitalRelationship>(YfMedicalWasteAndHospitalRelationship.class);
        return util.exportExcel(list, "medicalHospitalRelationship");
    }


    /**
     * @param mmap
     * @param currentDeptId 当前所选机构、暂存点 id
     * @return
     * 94720938-06f5-4739-a41d-514e9187598b
     */
    @GetMapping("/addOrEdit/{currentDeptId}")
    public String addOrEdit(ModelMap mmap, @PathVariable("currentDeptId") Long currentDeptId) {
//        List<SysDept> deptList = null;
        //判断当前用户所属机构是否已经签署三方合同
        //当前用户 机构id
        Long deptId = ShiroUtils.getSysUser().getDept().getDeptId();
        // 获取本机构或机构下未签署三方医废处理合同的的所有医院
//        deptList = deptService.selectMedicalWasteHospitals(currentDeptId, deptId);
//        SysDept deptList = deptService.selectDeptById(currentDeptId > 0 ? currentDeptId : deptId);

        //获取当前机构
        SysDept sysDept = deptService.selectCurrentDept(currentDeptId);

        //判断是编辑还是 新增
        //如果关系表中包含则返回编辑
        List<YfMedicalWasteAndHospitalRelationship> hospitalRelationships =
                yfMedicalWasteAndHospitalRelationshipService.selectYfMedicalWasteAndHospitalRelationshipList(new YfMedicalWasteAndHospitalRelationship(sysDept.getDeptId()));
        mmap.put("deptList", sysDept);

        if (hospitalRelationships.size() == 1) {//存在一个合同返回编辑页面
            mmap.put("yfMedicalWasteAndHospitalRelationship", hospitalRelationships.get(0));
            // 签署
            return prefix + "/edit";
        } else if (hospitalRelationships.size() > 1) {//多个合同，出现异常
            throw new BusinessException("医疗机构存在多份合同，请联系管理员");
        } else {//没有合同 返回添加页面.
            // 未签署
            return prefix + "/add";
        }
    }

    /**
     * 新增医废处置机构、医疗机构关系
     * 173594c7-608d-462a-ac28-b36eaf8c9d35
     */
    @GetMapping("/add/{deptId}")
    public String add(ModelMap mmap, @PathVariable("deptId") Long currentDeptId) {
        Long deptId = ShiroUtils.getSysUser().getDept().getDeptId();
        List<SysDept> deptList = deptService.selectMedicalWasteHospitals(currentDeptId, deptId);
        mmap.put("deptList", deptList);
        return prefix + "/add";
    }

    /**
     * 新增保存医废处置机构、医疗机构关系
     * 81cfcc1e-d4ef-4e83-9d07-8aa1ec730b58
     * 旧的医废处置机构、医疗机构关系
     */
//    @RequiresPermissions("yf:medicalHospitalRelationship:add")
    @Log(title = "医废处置机构、医疗机构关系", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YfMedicalWasteAndHospitalRelationship yfMedicalWasteAndHospitalRelationship) {
        return toAjax(yfMedicalWasteAndHospitalRelationshipService.insertYfMedicalWasteAndHospitalRelationship(yfMedicalWasteAndHospitalRelationship));
    }


    /**
     * 修改医废处置机构、医疗机构关系
     * 2c70fe5d-fc7a-4134-ba01-9861c8304219
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        YfMedicalWasteAndHospitalRelationship yfMedicalWasteAndHospitalRelationship = yfMedicalWasteAndHospitalRelationshipService.selectYfMedicalWasteAndHospitalRelationshipById(id);

        mmap.put("yfMedicalWasteAndHospitalRelationship", yfMedicalWasteAndHospitalRelationship);
        Long currentDeptId = yfMedicalWasteAndHospitalRelationship.getHospitalId();
//        SysDept deptList = deptService.selectDeptById(deptId);
        Long deptId = ShiroUtils.getSysUser().getDept().getDeptId();
        List<SysDept> deptList = deptService.selectMedicalWasteHospitals(currentDeptId, deptId);

        mmap.put("deptList", deptList);
        return prefix + "/edit";
    }

    /**
     * 修改保存医废处置机构、医疗机构关系
     * 01ac3d4b-50d8-41ed-bd01-06e3a20464a0
     */
//    @RequiresPermissions("yf:medicalHospitalRelationship:edit")
    @Log(title = "医废处置机构、医疗机构关系", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YfMedicalWasteAndHospitalRelationship yfMedicalWasteAndHospitalRelationship) {
        return toAjax(yfMedicalWasteAndHospitalRelationshipService.updateYfMedicalWasteAndHospitalRelationship(yfMedicalWasteAndHospitalRelationship));
    }

    /**
     * 删除医废处置机构、医疗机构关系
     * 81ace9ec-6867-405e-9c41-da5ac4b45f74
     */
    @RequiresPermissions("yf:medicalHospitalRelationship:remove")
    @Log(title = "医废处置机构、医疗机构关系", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(yfMedicalWasteAndHospitalRelationshipService.deleteYfMedicalWasteAndHospitalRelationshipByIds(ids));
    }
}
